<?php
include("include/config.inc.php");

if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  if(isset($_POST['submitBtn']))
  {
    $outgoingDate  	 = $_POST['outgoingDateYear']."-".$_POST['outgoingDateMonth']."-".$_POST['outgoingDateDay'];
    $partyId         = (isset($_POST['partyId'])) && ($_POST['partyId'] != "") ? $_POST['partyId'] : 0 ;
    $challan         = (isset($_POST['challan'])) && ($_POST['challan'] != "") ? $_POST['challan'] : 0 ;
    $outgoingChallan = (isset($_POST['outgoingChallan'])) && ($_POST['outgoingChallan'] != "") ? $_POST['outgoingChallan'] : "" ;
    
    $query = "INSERT INTO outgoing (outgoingDate,partyId,challan,outgoingChallan)
                   VALUES ('".$outgoingDate."',".$partyId.",".$challan.",'".$outgoingChallan."')";
    $queryRes = mysql_query($query);
    if(!$queryRes)
    {
     	die("Insert Query Not Inserted : ".mysql_error());
    }     
  }  
  //---------------------------------------------------------------
  {	
   	$catTitleArray = array();
   	$k = 0;
    $select = "SELECT partyId,partyName
               FROM party";
  	$selectResult = mysql_query($select);
  	while($row = mysql_fetch_array($selectResult))
  	{
    	$catTitleArray['partyId'][$k]   = $row['partyId'];
    	$catTitleArray['partyName'][$k] = $row['partyName'];
    	$k++;
  	}
  }
  //------------------------------------------------------------------
  $msg           = "Record Not Found";
  $incomingRegister = array();
	$i =0;
  $selectincomingRegister = "SELECT outgoingId,DATE_FORMAT(outgoing.outgoingDate,'%d-%m-%Y')AS outgoingDate,
                                    outgoing.outgoingChallan,party.partyName,outgoing.challan
															 FROM outgoing
													LEFT JOIN party ON party.partyId  = outgoing.partyId
													LEFT JOIN incoming ON incoming.incomingId = outgoing.incomingId
															ORDER BY party.partyName";
															 
	$selectincomingRegisterRes = mysql_query($selectincomingRegister);
	while($incomingRow = mysql_fetch_array($selectincomingRegisterRes))
	{
	 $incomingRegister[$i]['outgoingId']      = $incomingRow['outgoingId'];
	 $incomingRegister[$i]['outgoingDate']    = $incomingRow['outgoingDate'];
	 $incomingRegister[$i]['partyName']       = $incomingRow['partyName'];
	 $incomingRegister[$i]['challan']         = $incomingRow['challan'];
	 $incomingRegister[$i]['outgoingChallan'] = $incomingRow['outgoingChallan'];
	 $i++;
	}

	include("./bottom.php");
	$smarty->assign("catTitleArray",$catTitleArray);
	$smarty->assign("incomingRegister",$incomingRegister);
	$smarty->assign("msg",$msg);
	$smarty->display("outGoingEntry.tpl");
}
?>